package com.ljx.itbbs.server.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.itbbs.server.user.dao.PermsDao;
import com.ljx.itbbs.server.user.domain.entity.PermsCategoryEntity;
import com.ljx.itbbs.server.user.domain.entity.PermsEntity;
import com.ljx.itbbs.server.user.domain.vo.req.PermsSaveReq;
import com.ljx.itbbs.server.user.domain.vo.req.PermsUpdateReq;
import com.ljx.itbbs.server.user.domain.vo.resp.PermsCategoryResp;
import com.ljx.itbbs.server.user.domain.vo.resp.PermsResp;
import com.ljx.itbbs.server.user.domain.vo.resp.PermsTree;
import com.ljx.itbbs.server.user.service.PermsCategoryService;
import com.ljx.itbbs.server.user.service.PermsService;
import com.ljx.itbbs.server.user.service.adapter.PermsAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class PermsServiceImpl implements PermsService {

    @Autowired
    private PermsDao permsDao;

    @Autowired
    private PermsCategoryService permsCategoryService;

    @Override
    public Set<String> getPermission(Long uid) {
        return permsDao.getPermission(uid);
    }

    @Override
    public List<PermsEntity> getList() {
        return permsDao.list();
    }

    @Override
    public void savePerms(PermsSaveReq req) {
        // 判断是保存权限分类还是权限
        if(req.getPermsCategoryId() == 0) {
            permsCategoryService.savePermsCategory(PermsAdapter.buildPermsCategory(req));
        } else {
            PermsEntity permsEntity = BeanUtil.copyProperties(req, PermsEntity.class);
            permsDao.save(permsEntity);
        }
    }

    @Override
    public void updatePerms(PermsUpdateReq req) {
        permsDao.updatePerms(req);
    }

    @Override
    public List<PermsTree> getPermsList() {
        // 查询所有菜单分类
        List<PermsCategoryResp> tbPermsCategories = permsCategoryService.getPermsCategory();
        // 查询所有权限
        Map<Long, List<PermsEntity>> permsMap = getList()
                .stream()
                .collect(Collectors.groupingBy(PermsEntity::getPermsCategoryId));
        // 组装结果
        List<PermsTree> trees = new ArrayList<>();
        for (PermsCategoryResp permsCategoryResp: tbPermsCategories){
            PermsTree permsTree = new PermsTree();
            permsTree.setName(permsCategoryResp.getPermsName());
            permsTree.setId(permsCategoryResp.getId());
            permsTree.setStatus(permsCategoryResp.getStatus());
            permsTree.setCreateTime(permsCategoryResp.getCreateTime());
            permsTree.setPermsTrees(permsMap.get(permsCategoryResp.getId()));
            trees.add(permsTree);
        }
        return trees;
    }

    @Override
    public PermsResp getPermsInfo(Long permsId) {
        return BeanUtil.copyProperties(permsDao.getById(permsId), PermsResp.class);
    }

    @Override
    public List<PermsResp> getByRoleId(Long roleId) {
        return permsDao.getByRoleId(roleId);
    }
}
